/**
 * 
 */
package rachauto.dto;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import rachauto.persistence.jpa.model.GenericEntity;

/**
 * @author said
 *
 */
public class GenericMapper {
    
	public static GenericDto readAsDto(GenericEntity entity,Class<? extends GenericDto> clazz) {
		GenericDto instance = null;
		try {
			instance = clazz.newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		BeanUtils.copyProperties(entity, instance);
		return instance;
	}
	
	
	public static GenericEntity readAsEntity(GenericDto dto,Class<? extends GenericEntity> clazz) {
		GenericEntity instance = null;
		try {
			instance = clazz.newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		BeanUtils.copyProperties(dto, instance);
		return instance;
	}
	
	public static List<GenericEntity> readAsListEntities(List<GenericDto> listDto,Class<? extends GenericEntity> clazz) {
		List<GenericEntity> entities = new ArrayList<GenericEntity>();
		if(!CollectionUtils.isEmpty(listDto)){
			for(GenericDto dto : listDto){
				entities.add(GenericMapper.readAsEntity(dto,clazz));
			}
		}
	    return entities;	
	}
	
	public static List<? extends GenericDto> readAsListDtos(List<? extends GenericEntity> listEntities,Class<? extends GenericDto> clazz) {
		List<GenericDto> listDtos = new ArrayList<GenericDto>();
		if(!CollectionUtils.isEmpty(listEntities)){
			for(GenericEntity entity : listEntities){
				listDtos.add(GenericMapper.readAsDto(entity, clazz));
			}
		}
	    return listDtos;	
	}
	
	
	
}
